home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Demo's / Igor Demo Pro / 3 PutContentsIn Igor Pro Folder / Technical Notes / Igor Tech Notes / TN006 DSP support / FTMagPhase TEXT < prev    next >
Text File  |  1992-03-02  |  3KB  |  131 lines

  1.  
  2.  
  3. Function log2(val)
  4.     variable val
  5.     
  6.     return ln(val)/ln(2)
  7. end
  8.  
  9.  
  10. Function CeilPwr2(x)
  11.     variable x
  12.     
  13.     return 2^(ceil(log2(x)))
  14. end
  15.  
  16.  
  17.  
  18. | brings the first graph window containing the given wave to the front.  If no such window is found
  19. | then one is created.
  20. Proc BringDestFront(w)
  21.     string w
  22.     
  23.     string win
  24.     variable winIndex,didit=0
  25.     
  26.     CheckDisplayed /A $w
  27.     if(V_flag)                    | if displayed somewhere, need to know if is graph & bring to front if so
  28.         do
  29.             win=WinName(winIndex, 1)        | name of top graph window
  30.             if( cmpstr(win,"") == 0 )
  31.                 break;                            | no more graph wndows
  32.             endif
  33.             DoWindow /F $win
  34.             CheckDisplayed  $w
  35.             if(V_Flag)
  36.                 didit= 1
  37.                 break
  38.             endif
  39.             winIndex += 1
  40.         while(1)                | exit via break
  41.     endif
  42.     if(!didit)
  43.         Display $w                | if not displayed anywhere then ok to create a new window
  44.     endif
  45. end
  46.  
  47.  
  48. | Given an input wave ( doesn't have to be a power of two), create a new wave
  49. | with the suffix "_Mag" that contains the normalized frequency response and
  50. | optionally a wave with the suffix "_Phase".
  51. | Several levels of resolution enhancement (really just sin x/x interpolation)
  52. | are provided.
  53. | Options include windowing (Hann) vs no windowing, linear vs dB,
  54. | phase vs no phase
  55. | If phase then option of radians or degrees,wrapped or unwrapped
  56. | You may want to modify the code at the end of this macro.
  57. | It sets the display style and that is a matter of taste.
  58. | REQUIRES: BringDestFront(),CeilPwr2()
  59. |
  60. Macro FTMagPhase(w,window,resolution,linlog,phase,phasetype)
  61.     string w
  62.     Prompt w,"Input data:",popup WaveList("*",";","")
  63.     variable window=1
  64.     Prompt window,"Windowing:",popup "None;Hann"
  65.     variable resolution=1
  66.     Prompt resolution,"Resolution enhancement:",popup "none;2;4;8;16;32"
  67.     variable linlog= 2
  68.     Prompt linlog,"Magnitude mode:",popup "Linear;dB"
  69.     Variable phase= 1
  70.     Prompt phase,"Phase:",popup "No phase;Phase in radians;Phase in degrees"
  71.     Variable phasetype=1
  72.     Prompt phasetype,"Unwrap phase?",popup,"No;Yes"
  73. ;
  74.     PauseUpdate; Silent 1
  75.     
  76.     string destw=w+"_Mag",phasew= w+"_Phase"
  77.     Variable n= numpnts($w)
  78.     
  79.     if( (resolution<1) %| (resolution>6) )
  80.         Abort "resolution out of range"
  81.     endif
  82.     resolution -= 1
  83.     Duplicate/O $w $destw
  84.     if(window==2)
  85.         Hanning $destw; $destw *= 2            | assumes continuous rather than pulsed data
  86.     endif
  87.     Redimension/N=(CeilPwr2(n)*2^resolution) $destw        | pad with zeros to power of 2
  88.     fft $destw
  89.     $destw= r2polar($destw)
  90.     | NOTE: depending on your application you may want to un-comment the next line
  91. |    $destw[0] /= 2                                | dc is special
  92.     if( phase!=1 )
  93.         Duplicate/O $destw $phasew
  94.         Redimension/R $phasew
  95.         $phasew= imag($destw)
  96.         if( phasetype==2 )
  97.             $phasew[0]= $phasew[1]            | try to avoid glitch at dc
  98.             UnWrap 2*Pi,$phasew
  99.             $phasew[0]= 0
  100.         endif
  101.         if(phase==3)
  102.             $phasew *= 180/Pi
  103.             SetScale y,0,0,"deg",$phasew
  104.         else
  105.             SetScale y,0,0,"rad",$phasew
  106.         endif
  107.     endif
  108.     Redimension/R $destw
  109.     if( linlog==2 )
  110.         WaveStats/Q $destw
  111.         $destw= 20*log($destw/V_max)
  112.         SetScale y,0,0,"dB",$destw
  113.     else
  114.         $destw /= n/2
  115.         SetScale y,0,0,"V",$destw
  116.     endif
  117.     BringDestFront(destw)
  118.     if( phase!=1 )
  119.         CheckDisplayed  $phasew
  120.         if( !V_Flag )
  121.             Append/R $phasew
  122.         endif
  123.     endif
  124.     if( numpnts($destw) <= 129 )
  125.         Modify mode($destw)=4,marker($destw)=19,msize($destw)=1
  126.     else
  127.         Modify mode($destw)=0
  128.     endif
  129. end
  130.  
  131.